$(function(){
	//初始化日期控件
  	$('#bt, #et').datetimepicker({
  		language: 'zh-CN',
	    format: 'yyyy-mm-dd hh:00',
	    autoclose: true,
	    minView: "day"
	});
	
	//加载游戏下拉数据
	loadGamesKindName();
	//加载游戏平台下拉数据
	loadGamePlatform();
	//加载数据
	loadDatas();

	//推广号排序
	$("#gameIdSort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#gameIdSort span"));
	});
	//资产排序
	$("#scoreSort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#scoreSort span"));
	});
	//奖券排序
	$("#prizesSort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#prizesSort span"));
	});
	//充值排序
	$("#czSort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#czSort span"));
	});
	//转账排序
	$("#sendSort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#sendSort span"));
	});
	//注册时间排序
	$("#regTimeSort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#regTimeSort span"));
	});
	//最后登录时间排序
	$("#logTimeSort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#logTimeSort span"));
	});
	//当日输赢排序
	$("#todaySort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#todaySort span"));
	});
	//总输赢排序
	$("#allSySort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#allSySort span"));
	});
	//用户类型排序
	$("#typeSort").click(function(){
		$("#sorts").val($(this).attr("id"));
		initSort($(this));
		changeSortClass($("#typeSort span"));
	});
});

//加载玩家在线列表数据
function loadDatas(s){
	
	if(s=="search"){
		$("#page").val("");
	}
	
	//页码
	var page = $("#page").val();
	//一页显示的条数
	var pageSize = 100;
	//房间信息
	var game = $("#game").val();
	//注册时间
	var begin = $("#begin_time").val();
	var end = $("#end_time").val();
	//游戏平台
	var gamePlatform = $("#gamePlatform").val();
	//控制状态
	var state = $("#state").val();
	//作弊类型
	var zbType = $("#zbType").val();
	//用户类型
	var userType = $("#userType").val();
	//排序
	var sortkey = $("#sorts").val();
	var sortval = "";
	if(sortkey != ""){
		var val = $("#"+sortkey+" span").attr("class");
		if(val.indexOf("asc") != -1){
			sortval = "desc";
		}else if(val.indexOf("desc") != -1){
			sortval = "asc";
		}
	}
//	alert("sortkey: "+sortkey+"; sortval: "+sortval);
	$.ajax({
		type: "post",
		data: {
			"page":page,
			"pageSize":pageSize,
			"game":game,
			"begin":begin,
			"end":end,
			"gamePlatform":gamePlatform,
			"state":state,
			"zbType":zbType,
			"userType":userType,
			"sortKey":sortkey,
			"sortVal":sortval
		},
		url: "../../php/player/loadPlayerOnlineListData.php",
		beforeSend: function(){
			$("#load").css("display","block");
		},
		error:function () {
			// alert('no');
		},
		success: function(msg){
			// alert('ok');
			$("#load").css("display","none");
			
			//表格数据
			var datas = msg.records;
			var html = "";
			if(datas.length == 0){
				html += "<tr><td colspan='17' align='center'>没查询到符合条件的数据~</td></tr>";
			}else{
				for(var i = 0; i<datas.length; i++){
					//用户类型（0：普通用户；1：内部用户）
					var userKind = datas[i].userKind;
					var typeStr = "";
					
					var score = datas[i].score;//资产
					var zbl = datas[i].cheatRate//作弊率
					var realScore = datas[i].realScore;//总输赢
					
					if(userKind == "3"){// 银商 淡黄
						html += "<tr style='background-color:#FFFF00;'>";
						typeStr = "银商";
					}else if(userKind == "2"){//超R 紫色
						html += "<tr style='background-color:#DA12EC;'>";
						typeStr = "超R";
					}else if(userKind == "2" && realScore > 0){// 大R 总输赢大于0
						html += "<tr style='background-color:#eb5858;'>";
						typeStr = "大R";
					}else if(userKind == "2"){// 大R 淡红
						html += "<tr style='background-color:#FF9393;'>";
						typeStr = "大R";
					}else if(userKind == "1" && realScore > 0){// 小R 总输赢大于0
						html += "<tr style='background-color:#00cece;'>";
						typeStr = "小R";
					}else if(userKind == "1"){// 小R 淡蓝
						html += "<tr style='background-color:#00ffff;'>";
						typeStr = "小R";
					}else if(userKind == "5"){// 买币蝗虫 绿色
						html += "<tr style='background-color:#008000;'>";
						typeStr = "买币蝗虫";
					}else if(userKind == "0" && score >= 2000000){// 超过200万的蝗虫 紫色
						html += "<tr style='background-color:#a800ff;'>";
						typeStr = "蝗虫玩家";
					}else if(userKind == "0" && zbl != 0){// 被控制的蝗虫 土色
						html += "<tr style='background-color:#ffcc00;'>";
						typeStr = "蝗虫玩家";
					}else if(userKind == "0"){// 蝗虫 无色
						html += "<tr>";
						typeStr = "蝗虫玩家";
					}else if(userKind == "7"){// 内部用户
						html += "<tr style='background-color:#6570fd;'>";
						typeStr = "内部用户";
					}else{
						html += "<tr>";
						typeStr = "蝗虫玩家";
					}
					
					html += "<td>"+datas[i].zx+"</td>";//在线房间名 如果为空 为大厅
					html += "<td><a style='font-weight:bold; color:black;' href='./playerInfo/playerDetailsInfo.php?gameId="+datas[i].gameId+"' title='查看玩家详情'>"+datas[i].gameId+"</a></td>";//推广号
					html += "<td><a style='font-weight:bold; color:black;' href='./playerInfo/playerDetailsInfo.php?gameId="+datas[i].gameId+"' title='查看玩家详情'>"+datas[i].nickName+"</a></td>";//用户昵称
					html += "<td>"+score+"</td>";//资产
					html += "<td>"+datas[i].prizes+"</td>";//奖券
					html += "<td>"+datas[i].cz+"</td>";//充值
					html += "<td>"+datas[i].sendScore;+"</td>";//转账
					var regTime = datas[i].regTime;
					regTime = regTime.substring(0, regTime.lastIndexOf(":"));
					html += "<td>"+regTime+"</td>";//注册时间
					var lastLoginTime = datas[i].lastLoginTime;
					lastLoginTime = lastLoginTime.substring(0, lastLoginTime.lastIndexOf(":"));
					html += "<td>"+lastLoginTime+"</td>";//最后登陆时间
					html += "<td>"+datas[i].TodayScore+"</td>";//今日输赢
					html += "<td>"+realScore+"</td>";//总输赢
					
					//作弊控制（0：系统； 1：GM）
					var HandCheat = datas[i].HandCheat;
					if(HandCheat=="0"){
						HandCheat = "(系统)";
					}else if(HandCheat == "1"){
						HandCheat = "(GM)";
					}else if(HandCheat == "2"){
						HandCheat = "(循环)";
					}
					html += "<td>"+zbl+HandCheat+"</td>";//作弊率 万分制
					html += "<td>"+datas[i].limitScore+"</td>";//作弊限额
					html += "<td>"+datas[i].bloodScore+"</td>";//作弊状态
					html += "<td>"+datas[i].platFormName+"</td>";//游戏平台
					html += "<td>"+datas[i].packName+"</td>";//游戏平台					
					html += "<td>"+typeStr+"</td>";//用户类型
					html += "</tr>";
				}
			}
			$("#databody").html(html);
			
			//统计数据
			var countInfo = msg.countInfo;
			$("#score").html(countInfo[0].score==""?0:countInfo[0].score);//金币数
			$("#prizes").html(countInfo[0].prizes==""?0:countInfo[0].prizes);//奖券
			$("#cz").html(countInfo[0].cz==""?'￥0':'￥'+countInfo[0].cz);//充值
			$("#zsy").html(countInfo[0].zsy==""?0:countInfo[0].zsy);//总输赢
			$("#tdsy").html(countInfo[0].tdsy==""?0:countInfo[0].tdsy);//当日总输赢
			
			//分页数据
			var pageInfo = msg.pageInfo;
			//总条数
			var allCount = pageInfo[0].count;
			$("#count").html(allCount);
			//总页数
			var allPage = Math.ceil(allCount/pageSize);
			if(allPage==0){
				allPage = 1;
			}
			//当前页码
			var now = pageInfo[0].page;
			
			$("#page").val(now);
			$("#nowPage").html(now);
			$("#allPage").html(allPage);
			$("#pageIpu").val("");
		}
	});
	
}

function findAll(s){
	$("#load").css("display","block");
	
	$("#reset").click();
	
	loadDatas(s);
}

function loadGamesKindName(){
	$.ajax({
		type: "post",
		url: "../../php/sys/roomSet/loadGames.php",
		success: function(msg){
			var datas = msg.records;
			
			var html = "<option value=''>全部</option>";
			if(datas.length!=0){
				for(var i = 0; i < datas.length; i++){
					html += "<option value='"+datas[i].kindName+"'>"+datas[i].kindName+"</option>";
				}
			}
			$("#game").html(html);
		}
	});
}